package com.ky.manage.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ky.manage.R;
import com.ky.manage.model.response.ProductResp;

import java.util.List;

public class ProductsAdapter extends BaseAdapter {

    List<ProductResp.RowsDTO> mProducts;
    Context mContext;

    public ProductsAdapter(Context context, List<ProductResp.RowsDTO> products) {
        mContext = context;
        mProducts = products;
        Log.d("lhm", "ProductsAdapter mProducts size = " + mProducts.size());
    }

    @Override
    public int getCount() {
        return mProducts.size();
    }

    @Override
    public Object getItem(int position) {
        return mProducts.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.item_product, null);
            viewHolder = new ViewHolder();
            viewHolder.imageView = convertView.findViewById(R.id.iv_prduct_img);
            viewHolder.tvProductName = convertView.findViewById(R.id.tv_product_name);
            viewHolder.tvProductPrice = convertView.findViewById(R.id.tv_product_price);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ProductResp.RowsDTO product = mProducts.get(position);

        Glide.with(mContext).load(product.getShareIconUrl()).into(viewHolder.imageView);
        viewHolder.tvProductName.setText(product.getGoodsName());
        viewHolder.tvProductPrice.setText("¥"+product.getSellPrice());
        Log.d("lhm", "goods Name = " + product.getGoodsName());
        return convertView;
    }

    class ViewHolder{
        ImageView imageView;
        TextView tvProductName;
        TextView tvProductPrice;
    }

    public void setData(List<ProductResp.RowsDTO> products) {
        this.mProducts = products;
        notifyDataSetChanged();
    }
}
